package com.oneonefive.educationsystem.controller;

import com.oneonefive.educationsystem.domain.Login;
import com.oneonefive.educationsystem.domain.TeacherRequest;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.service.TeacherRequestService;
import com.oneonefive.educationsystem.vo.TeacherRequestVo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/teaReq")
public class TeacherRequestController {
    @Resource
    TeacherRequestService teacherRequestService;


    @PostMapping()
    @PreAuthorize("hasAuthority('teacher')")
    public Result teaReqSave(@RequestBody TeacherRequest teacherRequest,Authentication authentication) {
        Result legitimacy = teacherRequestService.legitimacy(teacherRequest);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        Login principal = (Login) authentication.getPrincipal();
        Result setValue = teacherRequestService.setValue(teacherRequest, principal.getUser());
        if (!setValue.getSuccess()) {
            return setValue;
        }
        boolean success = teacherRequestService.save(teacherRequest);
        System.out.println();
        if (success) {
            return Result.ok("teacherRequest新增成功");
        }
        return Result.fail("teacherRequest新增失败");
    }

    @PutMapping()
    @PreAuthorize("hasAuthority('teacher')")
    public Result teaReqUpdate(@RequestBody TeacherRequest teacherRequest,Authentication authentication) {
        Login principal = (Login) authentication.getPrincipal();
        Result setValue = teacherRequestService.setValue(teacherRequest, principal.getUser());
        if (!setValue.getSuccess()) {
            return setValue;
        }
        Result legitimacy = teacherRequestService.legitimacy(teacherRequest);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        boolean success = teacherRequestService.updateById(teacherRequest);
        if (success) {
            return Result.ok("teacherRequest修改成功");
        }
        return Result.fail("teacherRequest修改失败");
    }

    //data:List<TeacherRequest>
    @PostMapping("/1")
    @PreAuthorize("hasAuthority('teacher')")
    public Result teaReqList(@RequestBody TeacherRequestVo teacherRequestVo) {
        return teacherRequestService.teaReqList(teacherRequestVo);
    }

    //教师实验室申请状态更改为已批准
    @GetMapping("/{trId}")
    @PreAuthorize("hasAuthority('admin')")
    public Result setStateTo1(@PathVariable Integer trId) {
        TeacherRequest teacherRequest = teacherRequestService.getById(trId);
        teacherRequest.setState(1);
        boolean success = teacherRequestService.updateById(teacherRequest);
        if (success) {
            return Result.ok("教师实验室申请排课成功");
        }
        return Result.fail("教师实验室申请排课失败");
    }
}
